<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Cache;

class SystemStatisticsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        //24小时累加就是一天的统计
//        $this->systemStatisByDay();

        //每个小时的统计
        $this->systemStatisByHour();

        return $next($request);
    }

    //
    public function systemStatisByDay():void{
        $statisKey='system_views_day_'.date('Ymd');
        if(Cache::has($statisKey)){
            Cache::increment($statisKey);
        }else{
            //72*3600=259200
            Cache::put($statisKey,1,259200);
        }
    }
    //
    public function systemStatisByHour():void{
        $statisKey='system_views_hour_'.date('YmdH');
        if(Cache::has($statisKey)){
            Cache::increment($statisKey);
        }else{
            //48*3600=172800
            Cache::put($statisKey,1,172800);
        }
    }


}
